package leetcode.code0825;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	static int[][] range;
	static {
		range = new int[121][2];
		for (int age = 15; age <= 120; age++) {
			int left = (age >> 1) + 7;
			int right = age;
			if (age < 100) {
				right = Math.min(right, 100);
			}
			range[age][0] = left;
			range[age][1] = right;
		}
	}

	public int numFriendRequests(int[] ages) {
		int len = ages.length;
		if (len < 2) {
			return 0;
		}
		int[] group = new int[121];
		for (int age : ages) {
			group[age]++;
		}
		int[] sum = new int[121];
		int ans = 0;
		for (int age = 15; age < 121; age++) {
			sum[age] = group[age] + sum[age - 1];
			int people = group[age];
			int friendship = (sum[range[age][1]] - sum[range[age][0]] - 1) * people;
			ans += friendship;
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(2, this.numFriendRequests(HelpDebug.str2array3("[16,16]")));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(2, this.numFriendRequests(HelpDebug.str2array3("[16,16]")));
	}

	@Override
	public void debug3() {
		HelpDebug.compare(3, this.numFriendRequests(HelpDebug.str2array3("[20,30,100,110,120]")));
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
//		age[y] <= 0.5 * age[x] + 7
//		age[y] > age[x]
//		age[y] > 100 && age[x] < 100
//		for (int age = 15; age <= 120; age++) {
//			int left = (age >> 1) + 7;
//			int right = age;
//			if (age < 100) {
//				right = Math.min(right, 100);
//			}
//			System.out.println(age + " (" + left + "," + right + "]");
//		}
	}

}
